<?php

namespace App\Services;

use App\Http\Resources\Home\CashResource\CashCollection;
use App\Http\Resources\Seller\CashResource\CashCollection as SellerCashCollection;
use App\Http\Resources\Admin\CashResource\CashCollection as AdminCashCollection;
use App\Jobs\StoreDeposit;
use App\Models\Cash;
use App\Models\UserCheck;

class CashService extends BaseService
{


    // 获取提现列表
    public function getCash($auth = 'user')
    {
        $cash_model = new Cash();
        if ($auth == 'user') {
            $cash_model = $cash_model->where('user_id', '>', 0)->where('store_id', 0);
        } elseif ($auth == 'seller') {
            $cash_model = $cash_model
                ->where('user_id', 0)
                ->where('store_id', '>', 0);
        } elseif ($auth == 'admin') {
            $cash_model = $cash_model
                ->where('store_id', '>', 0);
        }
        $list = $cash_model->orderBy('id', 'desc')->paginate(request()->per_page ?? 30);

        if ($auth == 'user') {
            return $this->format(new CashCollection($list));
        }
        if ($auth == 'seller') {
            return $this->format(new SellerCashCollection($list));
        }
        return $this->format(new AdminCashCollection($list));
    }

    // money
    public function add($auth = 'user')
    {
        $cash_model = new Cash();
        $money      = abs(request()->money) + 0;

        // 金额不能小于100
        if ($money < 100) {
            return $this->format_error(__('users.cash_money_gt100'));
        }

        $ml_service = new MoneyLogService();

        if ($auth == 'user') {
            $user_service = new UserService;
            $user_info    = $user_service->getUserInfo();
            $uc_model     = new UserCheck();

            if ($money > $user_info['money']) {
                return $this->format_error(__('users.cash_money_insufficient'));
            }

            $uc_info = $uc_model->where('user_id', $user_info['id'])->first();
            if (!$uc_info) {
                return $this->format_error(__('base.error'));
            }
            $cash_model->user_id   = $user_info['id'];
            $cash_model->money     = $money;
            $cash_model->name      = $uc_info->name;
            $cash_model->card_no   = $uc_info->card_no;
            $cash_model->bank_name = $uc_info->bank_name;

            // 资金冻结处理

            $ml_service->editMoney(__('users.money_log_cash'), $user_info['id'], -$money);
            $ml_service->editMoney(__('users.money_log_cash'), $user_info['id'], $money, 1);
            $rs = $cash_model->save();
        }

        // 店铺提现
        if ($auth == 'seller') {
            $type = 2;  //1：自动提现  2：手动提现

            $store_info            = $this->get_store(false, 'id,store_money');
            //查询店铺的好评率
            $StoreService = new StoreService;
            $rate = $StoreService->getRate($store_info['id']);
            //查询设置的评分限制
            $grade = $StoreService->getStoreGrade($store_info['id']);
            if($rate>=$grade){
                $type = 1;
            }

            $cash_model->store_id  = $store_info['id'];
            $cash_model->money     = $money;
            $cash_model->name      = request()->name;
            $cash_model->card_no   = request()->bank_no;
            $cash_model->bank_name = request()->bank_name;
            $cash_model->bank_code = request()->bank_code;
            $cash_model->type = $type;
            $cash_model->cash_no = date('YmdHis') . $store_info['id'] . mt_rand(1000, 9999); // 生成订单号;

            if ($money > $store_info['store_money']) {
                return $this->format_error(__('users.cash_money_insufficient'));
            }
            // 扣余额
            $ml_service->editSellerMoney(__('users.money_log_cash'), $store_info['id'], -$money);
            // 冻结资金
            $ml_service->editSellerMoney(__('users.money_log_cash'), $store_info['id'], $money, 1);
            $rs = $cash_model->save();
            $insertId = $cash_model->id;

            //如果店铺好评率大于设置的评分则自动提现
            if($rate>=$grade){
                StoreDeposit::dispatch([$cash_model,1]);
            }

        }

        return $this->format($rs, __('users.cash_success'));
    }

    // 修改提现到账状态
    public function edit($id)
    {
        $cash_model  = new Cash();
        $cash_status = request()->cash_status;
        $refuse_info = request()->refuse_info ?? '';
        $cash_info = $cash_model->where('id', $id)->first();
        if($cash_status==1){ //通过就放入队列中
            StoreDeposit::dispatch([$cash_info,2]);
            $rs = [];
        }elseif($cash_status==2){

            if (!$cash_info) {
                return $this->format_error(__('base.error'));
            }
            $cash_info->cash_status = $cash_status;
            $cash_info->refuse_info = $refuse_info;
            $rs                     = $cash_info->save();

            $ml_service = new MoneyLogService();

            if ($cash_info->user_id > 0) {
                //用户提现
                $rs = $ml_service->editMoney(__('users.money_log_cash'), $cash_info['user_id'], -$cash_info['money'], 1, $refuse_info);
            } else {
                //商家提现
                $rs = $ml_service->editSellerMoney(__('users.money_log_cash'), $cash_info['store_id'], -$cash_info['money'], 1, $refuse_info, $cash_status);
                //若果拒绝提现
            }
        }



        // // 余额日志处理
        // if($auth == 'user'){
        //     $ml_service->editMoney(__('users.money_log_cash'),$cash_info['user_id'],-$cash_info['money'],1);
        // }

        // // 余额日志处理
        // if($auth == 'seller'){
        //     $ml_service->editSellerMoney(__('users.money_log_cash'),$cash_info['store_id'],-$cash_info['money'],1);
        // }

        return $this->format($rs);
    }
}
